{extend name="./base" /}

{block name="body"}
<div class="layui-fluid">
	<div class="layui-card">
		<!-- TABLE正文 -->
		<p style="padding: 15px 24px 0;">
			<a class="layui-btn" id="saveBtn">保存</a>
		</p>
		<div class="layui-card-body">
			<ul id="authRuleTree" class="ztree"></ul>
		</div>

	</div>
	<input type="hidden" id="gid" value="{$id}" />
</div>
{/block}

{block name="script"}
<script>
	layui.config({
		base: '__STATIC__/layuiadmin/lib/extend/' //静态资源所在路径
	}).extend({
		zTree: 'zTree/zTree'
	}).use(['layer', 'form', 'table', 'zTree'], function () {
		var $ = layui.jquery, layer = layui.layer;
		let gid = $("#gid").val();
		$.post('/admin/rbac/grouprules', {gid: gid}, function(res){
			let insTree = $.fn.zTree.init($('#authRuleTree'), {
				check: {enable: true},
				data: {key: {name: "title"}, simpleData: {enable: true, pIdKey: "pid"}},
				callback: {
					onClick: function (e, treeId, treeNode, clickFlag) {
						$.fn.zTree.getZTreeObj("roleAuthTree").checkNode(treeNode, !treeNode.checked, true);
					}
				}
			}, res.data);
			insTree.expandAll(true);
		})
		$("#saveBtn").click(function(){
			let insTree = $.fn.zTree.getZTreeObj('authRuleTree');
			let checkedRows = insTree.getCheckedNodes(true);
			let ids = [];
			for (let i = 0; i < checkedRows.length; i++) {
				ids.push(checkedRows[i].id);
			}
			$.post('/admin/rbac/saverules', {ids: ids, gid: gid}, function(){
				layer.msg('保存成功', {time: 1000}, function(){
					window.location.href = '/admin/rbac/role'
				})
			})
		})
	})
</script>
{/block}